package yamen.bdwm.board;

import java.util.ArrayList;

import yamen.bdwm.R;
import yamen.bdwm.ThreadsActivity;
import yamen.bdwm.compatible.ActionBarActivity;
import yamen.bdwm.data.WmMyData;
import yamen.bdwm.data.interfaces.onListRefreshedListener;
import yamen.bdwm.datastruct.MoreAdapter.onMoreOver;
import yamen.bdwm.datastruct.WmBoardItem;
import yamen.bdwm.datastruct.WmDataAdapter;
import yamen.bdwm.datastruct.WmItem;
import yamen.bdwm.datastruct.WmMoreList;
import yamen.bdwm.datastruct.WmThreadItem;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ThreadsListFragment extends ListFragment implements
		onListRefreshedListener,GetListName{
	WmMoreList wmList;
	private int skip = 0;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.list, container, false);
		wmList=new WmMoreList() {
			
			@Override
			protected void loadForNumber(onMoreOver l) {
				new GetThreadsTask(this, skip, l).execute();
			}
		};
		
		wmList.CreateAdapter(0, R.layout.board_content_item);
		
		final WmDataAdapter adapter = wmList.getAdapter(0,this.getActivity());

		((ListView) v.findViewById(R.id.listView1)).setAdapter(adapter);
		
		((ListView) v.findViewById(R.id.listView1))
				.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						WmThreadItem item =(WmThreadItem)adapter.getItem(position);  
						if(item.status.contains("N"))
						{
							item.status = item.status.replace("N",""); 
							wmList.NotifyAllAdapter();
						}
						((ThreadsActivity)getActivity()).showThread(((WmItem)adapter.getItem(position)).url,((WmItem)adapter.getItem(position)).title);
						}
					

				});
		Bundle bundle = getArguments();
		if (bundle != null) {
			String boardname=bundle
					.getString("board");
			
			boardItem = WmMyData.getinstance().boardmapHashMap.get(boardname);
		if (adapter.getCount() == 0) {
			((ActionBarActivity)this.getActivity()).SetRefreshingDialog(true);
			new GetThreadsTask(wmList, 0,null).execute();
		}
		}
		
		return v;

	}
	WmBoardItem boardItem;
	public void setBoard(WmBoardItem wmb)
	{
		Bundle bundle = new Bundle();
		if(wmb.getIndexName() != null)
			bundle.putString("board", wmb.getIndexName());
		
		this.setArguments(bundle);
	}
	public void onRefreshed(int numberOfRefreshed) {
		((ActionBarActivity)this.getActivity()).SetRefreshingDialog(false);
		if (getActivity() instanceof ActionBarActivity) {
			((ActionBarActivity) getActivity()).getActionBarHelper()
					.setRefreshActionItemState(false);
		}
	}
	public void onError(int i) {
		((ActionBarActivity)this.getActivity()).SetRefreshingDialog(false);
		if (getActivity() instanceof ActionBarActivity) {
			((ActionBarActivity) getActivity()).getActionBarHelper()
					.setRefreshActionItemState(false);
		}
		
	}
	public void getRefreshed(boolean modeChanged)
	{
		((ActionBarActivity)this.getActivity()).SetRefreshingDialog(true);
		wmList.Clear();
		wmList.NotifyAllAdapter();
		skip = 0;
		new GetThreadsTask(wmList, 0,null).execute();
	}
	class GetThreadsTask extends AsyncTask<Void, Void, Void>
	{
		WmMoreList wmList;
		int num;
		onMoreOver l;
		GetThreadsTask(WmMoreList wmlist,int n,onMoreOver l)
		{
			wmList=wmlist;
			num=n;
			this.l=l;
		}
		@Override
		protected Void doInBackground(Void... params) {
			ArrayList<WmItem> d=WmMyData.getinstance().parser.getThreads(boardItem.boardNameEng, num);
			if(d == null)
				return null;
			for(int i=0;i<d.size();i++)
			{
				wmList.Add(d.get(i));
				if(WmMyData.getinstance().boardMode == 1)
				{
					int index = 0;
					try {
						index = Integer.parseInt(((WmThreadItem)d.get(i)).index);
					} catch (Exception e) {
						index = 0;
					}
					if(num  <= 10 || num > index)
						num = index;
				}
			}
			return null;
		}
		protected void onPostExecute(Void s)
		{
			if(WmMyData.getinstance().boardMode == 0)
				skip += 20;
			else {
				 skip = num;
			}
			wmList.NotifyAllAdapter();
			if(l!=null)
				l.OnMoreOver();
			if((ActionBarActivity)getActivity()!=null)
				((ActionBarActivity)getActivity()).SetRefreshingDialog(false);
		}
	}
	public WmBoardItem getListItem() {
		return boardItem;
	}

}
